package dao;

import entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tool.MyBatisUtil;

import java.io.InputStream;
import java.util.Map;

public class UserDaoImpl implements UserDao {


    public int getCount() {
        int result=0;
        //sqlSession--sqlSesssionFactory--sqlSesssionFactoryBuilder
        try{
            //读取mybatis的配置文件
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建sqlSesssionFactoryBuilder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建sqlSesssionFactory
            SqlSessionFactory factory=builder.build(is);
            //创建sqlSession crud
            SqlSession sqlSession=factory.openSession();
            //dao接口的全限定名+id名
            //sqlsession操作数据库 有两种方法
            //第一种  内置实例方法 selectOne selectList select update delete insert
            //Mapper接口   getMapper
            //result=sqlSession.selectOne("dao.UserDao.all");
            //要求：方法名要和id名一致
            result=sqlSession.getMapper(UserDao.class).getCount();
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return  -1;
        }
    }

    @Override
    public User getUsersByUsername(String username) {
        SqlSession sqlSession=null;
        User user=null;
        try{
            sqlSession= MyBatisUtil.getSession();
            user=sqlSession.getMapper(UserDao.class).getUsersByUsername(username);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return user;
    }

    @Override
    public int checklogin(String username, String pwd) {
        SqlSession sqlSession=null;
       int result=-1;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UserDao.class).checklogin(username,pwd);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public int checklogin2(User user) {
        SqlSession sqlSession=null;
        int result=-1;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UserDao.class).checklogin2(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public int checklogin3(User user) {
        SqlSession sqlSession=null;
        int result=-1;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UserDao.class).checklogin3(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    @Override
    public int checklogin4(Map map) {
        SqlSession sqlSession=null;
        int result=-1;
        try{
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(UserDao.class).checklogin4(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }
}
